package interesado;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import misc.Logica;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class ModificarCuenta_GUI extends javax.swing.JFrame{

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel lblCartel;
	private JLabel lblNombre;
	private JTextField txtTelefono;
	private JButton btnAceptar;
	private JButton btnCancelar;
	private JPasswordField txtPasswordConfirmacion;
	private JPasswordField txtPassword;
	private JTextField txtDNI;
	private JLabel lblDNI;
	private JTextField txtNombre;
	private JTextField txtEmail;
	private JTextField txtApellido;
	private JTextField txtLU;
	private JLabel lblPasswordConfirmacion;
	private JLabel lblPassword;
	private JLabel lblEmail;
	private JLabel lblTelefono;
	private JLabel lblLU;
	private JLabel lblApellido;

	//Agregado
	private Interesado_GUI padre;
	private Logica logica;
	/**
	* Auto-generated main method to display this JFrame
	*/
	
	public ModificarCuenta_GUI(Interesado_GUI padre, Logica logica) {
		super();
		initGUI();
		this.padre=padre;
		this.logica=logica;
	}
	
	
	public void llenarCampos(String [] arr) {
		txtApellido.setText(arr[0]);
		txtNombre.setText(arr[1]);
		txtDNI.setText(arr[2]);
		txtEmail.setText(arr[3]);
		txtTelefono.setText(arr[4]);
		txtPassword.setText(arr[5]);
		txtPasswordConfirmacion.setText(arr[5]);
		txtLU.setText(arr[6]);
		
	}
	
	
	private void initGUI() {
		try {
			setTitle("Modificar Datos");
			setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				lblCartel = new JLabel();
				getContentPane().add(lblCartel);
				lblCartel.setText("Modificar Datos");
				lblCartel.setBounds(47, 31, 129, 21);
				lblCartel.setFont(new java.awt.Font("Segoe UI",0,16));
			}
			{
				lblNombre = new JLabel();
				getContentPane().add(lblNombre);
				lblNombre.setText("Nombre:");
				lblNombre.setBounds(47, 79, 89, 16);
				lblNombre.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblApellido = new JLabel();
				getContentPane().add(lblApellido);
				lblApellido.setText("Apellido:");
				lblApellido.setBounds(33, 107, 103, 16);
				lblApellido.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblDNI = new JLabel();
				getContentPane().add(lblDNI);
				lblDNI.setText("DNI:");
				lblDNI.setBounds(89, 135, 47, 16);
				lblDNI.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblLU = new JLabel();
				getContentPane().add(lblLU);
				lblLU.setText("LU:");
				lblLU.setBounds(89, 163, 47, 16);
				lblLU.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblTelefono = new JLabel();
				getContentPane().add(lblTelefono);
				lblTelefono.setText("Teléfono:");
				lblTelefono.setBounds(68, 191, 68, 16);
				lblTelefono.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblEmail = new JLabel();
				getContentPane().add(lblEmail);
				lblEmail.setText("E-mail:");
				lblEmail.setBounds(68, 219, 68, 16);
				lblEmail.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblPassword = new JLabel();
				getContentPane().add(lblPassword);
				lblPassword.setText("Password:");
				lblPassword.setBounds(68, 247, 68, 16);
				lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblPasswordConfirmacion = new JLabel();
				getContentPane().add(lblPasswordConfirmacion);
				lblPasswordConfirmacion.setText("Confirme Password:");
				lblPasswordConfirmacion.setBounds(12, 282, 124, 16);
				lblPasswordConfirmacion.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				txtNombre = new JTextField();
				getContentPane().add(txtNombre);
				txtNombre.setBounds(148, 76, 232, 23);
				txtNombre.setEditable(false);
				txtNombre.setEnabled(false);
			}
			{
				txtLU = new JTextField();
				getContentPane().add(txtLU);
				txtLU.setBounds(148, 160, 232, 23);
				txtLU.setEditable(false);
				txtLU.setEnabled(false);
			}
			{
				txtApellido = new JTextField();
				getContentPane().add(txtApellido);
				txtApellido.setBounds(148, 105, 232, 23);
				txtApellido.setEditable(false);
				txtApellido.setEnabled(false);
			}
			{
				txtEmail = new JTextField();
				getContentPane().add(txtEmail);
				txtEmail.setBounds(148, 216, 232, 23);
			}
			{
				txtDNI = new JTextField();
				getContentPane().add(txtDNI);
				txtDNI.setBounds(148, 132, 232, 23);
				txtDNI.setEditable(false);
				txtDNI.setEnabled(false);
			}
			{
				txtTelefono = new JTextField();
				getContentPane().add(txtTelefono);
				txtTelefono.setBounds(148, 188, 232, 23);
			}
			{
				txtPassword = new JPasswordField();
				getContentPane().add(txtPassword);
				txtPassword.setBounds(148, 250, 232, 23);
				txtPassword.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						txtPasswordKeyReleased(evt);
					}
				});
			}
			{
				txtPasswordConfirmacion = new JPasswordField();
				getContentPane().add(txtPasswordConfirmacion);
				txtPasswordConfirmacion.setBounds(148, 279, 232, 23);
				txtPasswordConfirmacion.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						txtPasswordConfirmacionKeyReleased(evt);
					}
				});
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(289, 323, 91, 23);
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						cancelar();
					}
				});
			}
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar);
				btnAceptar.setText("Aceptar");
				btnAceptar.setBounds(176, 323, 85, 23);
				btnAceptar.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent evt) {
							modificarAceptar();
							
					}
				});
			}
			pack();
			this.setSize(455, 408);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

	
	//Metodos agregados
	
	private void cancelar(){
		padre.setVisible(true);
		this.setVisible(false);
	}
	
	
private void modificarAceptar() {
	//Validamos
	String nombre=txtNombre.getText();
	String apellido=txtApellido.getText();
	String email=txtEmail.getText();
	String password=txtPassword.getText();
	String confirmacionPassword=txtPasswordConfirmacion.getText();
	String telefono=txtTelefono.getText();
	
	if(email.equals("")||password.equals("")||confirmacionPassword.equals("")||telefono.equals(""))
		JOptionPane.showMessageDialog(null,"debe completar todos los campos","Error",JOptionPane.ERROR_MESSAGE);
	else if(!txtPassword.getText().equals(txtPasswordConfirmacion.getText()))
		JOptionPane.showMessageDialog(null,"el password y su confirmación son diferentes","Error",JOptionPane.ERROR_MESSAGE);
	else
		
	{
		 int respuesta = JOptionPane.showConfirmDialog(null, "¿Confirma la modificación?", "Modificar Datos",
                 JOptionPane.YES_NO_OPTION);
		 if(respuesta==JOptionPane.YES_OPTION){
		
		int res=logica.modificarCuenta(telefono,email,password);
		if(res==-1)
			JOptionPane.showMessageDialog(null,"Se produjo un error al modificar los datos", "Error", JOptionPane.ERROR_MESSAGE);
		else{
			JOptionPane.showMessageDialog(null,"La operación se realizó correctamente", "Modificar datos", JOptionPane.INFORMATION_MESSAGE);
			this.setVisible(false);
			padre.setVisible(true);
		}
		
		}
	}
}


//Validacion de password

private boolean isPasswordCorrect(char[] j1,char[] j2) {
	boolean valor = true;
	int puntero = 0;
	if (j1.length != j2.length){
	valor = false;
	}
	else{
	while((valor)&&(puntero < j1.length)){
	if (j1[puntero] != j2[puntero]){
	valor = false;
	}
	puntero++;
	}
	}
	return valor;
	}


private void comparaPass() {
	if(!isPasswordCorrect(txtPassword.getPassword(),txtPasswordConfirmacion.getPassword()))
	{
		txtPasswordConfirmacion.setBackground(Color.red);
	btnAceptar.setEnabled(false );
}
		else
			{
			btnAceptar.setEnabled(true);
			
			txtPasswordConfirmacion.setBackground(Color.green);
			}
}


private void txtPasswordConfirmacionKeyReleased(KeyEvent evt) {
	comparaPass();
	
			
	
		
}

private void txtPasswordKeyReleased(KeyEvent evt) {
	comparaPass();
}

}
